package com.iplay.service.order.query;

import java.util.Collection;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.data.jpa.domain.Specification;

import com.iplay.entity.order.OrderDO;
import com.iplay.entity.order.OrderStatus;

public class OrderQueryByStatusAndParticipant implements Specification<OrderDO> {

	private Collection<OrderStatus> orderStatus;

	private int userId;

	public OrderQueryByStatusAndParticipant(Collection<OrderStatus> orderStatus, int userId) {
		this.orderStatus = orderStatus;
		this.userId = userId;
	}

	@Override
	public Predicate toPredicate(Root<OrderDO> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
		return cb.and(root.get("orderStatus").in((orderStatus)), cb.or(cb.equal(root.get("customerId"), userId),
				cb.equal(root.get("recommenderId"), userId), cb.equal(root.get("managerId"), userId)));
	}

}
